package cn.iocoder.yudao.module.hs.service.shipordertrayrecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.hs.dal.dataobject.picktaskmaterialdetail.PickTaskMaterialDetailDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.shiporder.ShipOrderDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.tray.TrayDO;
import cn.iocoder.yudao.module.hs.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.hs.dal.mysql.picktaskmaterialdetail.PickTaskMaterialDetailMapper;
import cn.iocoder.yudao.module.hs.service.shiporder.ShipOrderService;
import cn.iocoder.yudao.module.hs.service.tray.TrayService;
import cn.iocoder.yudao.module.hs.service.warehouse.WarehouseService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.hs.controller.admin.shipordertrayrecord.vo.*;
import cn.iocoder.yudao.module.hs.dal.dataobject.shipordertrayrecord.ShipOrderTrayRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.hs.dal.mysql.shipordertrayrecord.ShipOrderTrayRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hs.enums.ErrorCodeConstants.*;

/**
 * 出库单托盘记录 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ShipOrderTrayRecordServiceImpl implements ShipOrderTrayRecordService {

    @Resource
    private ShipOrderTrayRecordMapper shipOrderTrayRecordMapper;
    @Resource
    private ShipOrderService shipOrderService;
    @Resource
    private WarehouseService warehouseService;
    @Resource
    private TrayService trayService;
    @Resource
    private PickTaskMaterialDetailMapper pickTaskMaterialDetailMapper;

    @Override
    public Long createShipOrderTrayRecord(ShipOrderTrayRecordSaveReqVO createReqVO) {
        ShipOrderDO shipOrder = shipOrderService.getShipOrder(createReqVO.getShipOrderId());
        if (shipOrder == null) {
            throw exception(SHIP_ORDER_NOT_EXISTS);
        }
        WarehouseDO warehouse = warehouseService.getWarehouse(createReqVO.getWarehouseId());
        if (warehouse == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
        if (!shipOrder.getWarehouseId().equals(warehouse.getId())) {
            throw exception(SHIP_ORDER_WAREHOUSE_NOT_MATCH);
        }
        TrayDO tray = trayService.getTray(createReqVO.getInfoCode());
        if (tray == null) {
            List<PickTaskMaterialDetailDO> pickTaskMaterialDetailDOS = pickTaskMaterialDetailMapper.selectList(PickTaskMaterialDetailDO::getRfidCode, createReqVO.getInfoCode());
            if (CollUtil.isNotEmpty(pickTaskMaterialDetailDOS)) {
                tray = trayService.getTray(pickTaskMaterialDetailDOS.get(0).getTrayCode());
            }
        }
        if (tray == null) {
            throw exception(TRAY_NOT_EXISTS);
        }
        List<ShipOrderTrayRecordDO> shipOrderTrayRecordDOS = shipOrderTrayRecordMapper.selectList(ShipOrderTrayRecordDO::getShipOrderId, createReqVO.getShipOrderId(), ShipOrderTrayRecordDO::getTrayCode, tray.getCode());
        if (CollUtil.isNotEmpty(shipOrderTrayRecordDOS)) {
            throw exception(SHIP_ORDER_TRAY_RECORD_EXISTS);
        }
        // 插入
        ShipOrderTrayRecordDO shipOrderTrayRecord = BeanUtils.toBean(createReqVO, ShipOrderTrayRecordDO.class);
        shipOrderTrayRecord.setTrayCode(tray.getCode());
        shipOrderTrayRecord.setStatus(1);
        shipOrderTrayRecordMapper.insert(shipOrderTrayRecord);
        // 返回
        return shipOrderTrayRecord.getId();
    }

    @Override
    public void updateShipOrderTrayRecord(ShipOrderTrayRecordSaveReqVO updateReqVO) {
        // 校验存在
        validateShipOrderTrayRecordExists(updateReqVO.getId());
        // 更新
        ShipOrderTrayRecordDO updateObj = BeanUtils.toBean(updateReqVO, ShipOrderTrayRecordDO.class);
        shipOrderTrayRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteShipOrderTrayRecord(Long id) {
        // 校验存在
        validateShipOrderTrayRecordExists(id);
        // 删除
        shipOrderTrayRecordMapper.deleteById(id);
    }

    private void validateShipOrderTrayRecordExists(Long id) {
        if (shipOrderTrayRecordMapper.selectById(id) == null) {
            throw exception(SHIP_ORDER_TRAY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShipOrderTrayRecordDO getShipOrderTrayRecord(Long id) {
        return shipOrderTrayRecordMapper.selectById(id);
    }

    @Override
    public PageResult<ShipOrderTrayRecordDO> getShipOrderTrayRecordPage(ShipOrderTrayRecordPageReqVO pageReqVO) {
        return shipOrderTrayRecordMapper.selectPage(pageReqVO);
    }

}